﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicinaMuncii.Entities;
using MedicinaMuncii.BL.Contracts;
using MedicinaMuncii.IoC;

namespace MedicinaMuncii.UI.Mvc.Controllers
{
    public class CompanyController : BaseController
    {
        ICompanyService companyService;
        IAddressService addressService;
        IInsultService insultsService;
        ICompanyInsultService companyInsultService;

        #region Constructor
        public CompanyController()
        {
            companyService = IoCWorker.Resolve<ICompanyService>();
            addressService = IoCWorker.Resolve<IAddressService>();
            insultsService = IoCWorker.Resolve<IInsultService>();
            companyInsultService = IoCWorker.Resolve<ICompanyInsultService>();
        }
        #endregion Constructor

        public ActionResult Index()
        {
            IList<Company> companies = companyService.GetAll("employees");
            ViewData["Insults"] = insultsService.GetAll();
            return View(companies);
        }

        public ActionResult Create()
        {
            ViewData["Insults"] = insultsService.GetAll();
            return View();
        }

        [HttpPost]
        public ActionResult Create(Company company, FormCollection form)
        {
            ViewData["Insults"] = insultsService.GetAll();

            try
            {
                companyService.Save(company);

                // Save the insults
                foreach (string insultKey in form.AllKeys.Where(k => k.Contains("Insult")))
                {
                    string insultFormValue = form[insultKey];

                    if (insultFormValue.Contains(','))
                    {
                        insultFormValue = insultFormValue.Substring(0, insultFormValue.IndexOf(','));
                    }

                    bool insultSelected = false;
                    Boolean.TryParse(insultFormValue, out insultSelected);

                    if (insultSelected)
                    {
                        CompanyInsult companyInsult = new CompanyInsult();
                        companyInsult.InsultID = Convert.ToInt32(insultKey.Replace("Insult", string.Empty));
                        companyInsult.CompanyID = company.ID;
                        companyInsultService.Add(companyInsult);
                    }
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            ViewData["Insults"] = insultsService.GetAll();
            Company company = companyService.GetByID(c => c.ID == id, new string[] { "address", "companyinsults.insult" });
            return View(company);
        }

        [HttpPost]
        public ActionResult Edit(Company company, FormCollection form)
        {
            ViewData["Insults"] = insultsService.GetAll();

            try
            {
                // Save the address
                Address address = company.address;
                address.companies.Clear();
                address.AcceptChanges();

                if (address.ID == 0)
                {
                    address.ChangeTracker.State = ObjectState.Added;
                }
                else
                {
                    address.ChangeTracker.State = ObjectState.Modified;
                }

                addressService.Save(address);

                // Save the company
                company.address = address;
                company.AcceptChanges();
                company.ChangeTracker.State = ObjectState.Modified;
                companyService.Save(company);

                // Save the insults
                foreach (CompanyInsult insult in companyInsultService.Find(ci => ci.CompanyID == company.ID))
                {
                    companyInsultService.Delete(insult);
                }

                foreach (string insultKey in form.AllKeys.Where(k => k.Contains("Insult")))
                {
                    string insultFormValue = form[insultKey];

                    if (insultFormValue.Contains(','))
                    {
                        insultFormValue = insultFormValue.Substring(0, insultFormValue.IndexOf(','));
                    }
                    
                    bool insultSelected = false;
                    Boolean.TryParse(insultFormValue, out insultSelected);

                    if (insultSelected)
                    {
                        CompanyInsult companyInsult = new CompanyInsult();
                        companyInsult.InsultID = Convert.ToInt32(insultKey.Replace("Insult", string.Empty));
                        companyInsult.CompanyID = company.ID;
                        companyInsultService.Add(companyInsult);
                    }
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            ViewData["Insults"] = insultsService.GetAll();
            companyService.Delete(c => c.ID == id);
            return RedirectToAction("Index");
        }
    }
}
